<?php

/**
 *
 */ 
class Shipping extends BaseShipping
{
    public function getPriceFmt()
    {
        return number_format($this->price / 100, 2);
    }
    public function hydrate(ResultSet $rs, $startcol = 1)
    {
        $this->setCulture(sfContext::getInstance()->getUser()->getCulture());
        parent::hydrate($rs, $startcol);
    }
    /**
     * 
     * returns prices list for shipping
     */
    public static function getApprovedShippings($countryId = '')
    {
    	$arSh = array();
        $c = new Criteria();
        if($countryId == 'LT')
        {
        	$c->add(ShippingPeer::IN_SAME_CIUNTRY, MyConstants::STATUS_APPROVED);
        }
        elseif($countryId != ''){ 
        	$c->add(ShippingPeer::IN_SAME_CIUNTRY, MyConstants::STATUS_DELETED);
        }
        $c->add(ShippingPeer::STATUS_ID, MyConstants::STATUS_APPROVED);
        $arShippings = ShippingPeer::doSelect($c);
        if(!empty($arShippings))
        {
        	foreach($arShippings as $shipping)
        	{
        		use_helper('Number');
        		$arSh[$shipping->getId()]= $shipping->getText() . ' - ' 
        		. format_currency($shipping->getPriceByCurrencyCode(sfContext::getInstance()->getUser()->getAttribute('currency', 'EUR'))
        						, sfContext::getInstance()->getUser()->getAttribute('currency', 'EUR'));
        	}
        }
        return $arSh;
    }
    /**
     * 
     * converts price to wanted currency
     * @param string $currencyCode
     */
	public function getPriceByCurrencyCode($currencyCode = 'LTL')
    {
        $arConverter = MyConstants::getCurrencyConverter();
        $currency = $this->getCurrency();
        $toCurrency = $currencyCode;
        $factorKey = $currency . '_' . $toCurrency;
        $price = $this->getPriceFmt();
        if( !empty($arConverter[$factorKey]) && ($arConverter[$factorKey] != $currency) )
        {
            $price = $this->getPriceFmt() * $arConverter[$factorKey];
        }
        return $price;
    }
}
